<script setup lang="js">
import { reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import Request from '@/api/axios'
import dayjs from 'dayjs'
import hljs from 'highlight.js'
import CopyIcon from '@/components/icons/CopyIcon.vue'
import { message } from 'ant-design-vue'
import { storeToRefs } from 'pinia'
import { useClipboard } from '@vueuse/core'
import { HttpStatusCode } from 'axios'
import { safeJsonParse } from '@/utils/safeJson'
const columns = [
    {
        title: '运行时间',
        dataIndex: 'time',
        key: 'time',
    },
    {
        title: '占用内存',
        dataIndex: 'memory',
        key: 'memory',
    },
    {
        title: '状态',
        dataIndex: 'status',
        key: 'status'
    }
]
const route = useRoute()
const router = useRouter()

const record = reactive({
    id: '',
    title: '',
    problem_id: '',
    created_at: '',
    username: '',
    user_id: '',
    code: '',
    language: '',
    judge_result: '',
    verdict: '',
    max_time: 0,
    max_memory: 0
})

const id = route.params.id

onMounted(async () => {
    await Request.get({
        url: `/record/${id}`
    })
        .then((response) => {
            if (response.code === HttpStatusCode.NotFound) {
                router.push({ name: 'notfound' })
                return
            }
            let items = response.info
            record.id = id
            record.title = items.problem_title ?? 'problem_title'
            record.problem_id = items.problem_id
            record.time = items.created_at
            record.username = items.username ?? 'username'
            record.user_id = items.user_id
            record.code = items.code
            record.language = items.language
            if (typeof items.judge_result === 'string') {
                record.judge_result = safeJsonParse(items.judge_result).value
            } else record.judge_result = items.judge_result
        })
        .catch((err) => {
            message.error('记录获取失败')
            console.log(err)
        })
    hljs.highlightAll()
})


const { text, copy, copied, isSupported } = useClipboard({ record })
const colorMap = {
    'Accepted': '#36c165',
    'WrongAnswer': '#f76262',
    'TimeLimitExceeded': '#f9934a'
}
const getTextColor = (status) => {
    return colorMap[status] || 'black'
}
</script>

<template>
    <a-card style="width: 90%" class="mt-12" shadow="hover">
        <div class="info-container">
            <h1 style="font-family: Menlo; color: #2f54eb">{{ record.title }}</h1>
            <div class="info-row">
                <span class="info-label">提交者:</span>
                <span class="info-value">{{ record.username }}</span>
            </div>
            <div class="info-row">
                <span class="info-label">提交时间:</span>
                <span class="info-value">{{
                    dayjs(record.time).format('YYYY/MM/DD HH:mm:ss')
                    }}</span>
            </div>
            <div class="info-row">
                <span class="info-label">语言:</span>
                <span class="info-value">{{ record.language }}</span>
            </div>
            <div class="info-row">
                <span class="info-label">代码长度:</span>
                <span class="info-value">{{ record.code.length }} B</span>
            </div>
            <div class="info-row">
                <span class="info-label">运行时间:</span>
                <span class="info-value">{{ record.max_time }} s</span>
            </div>
            <div class="info-row">
                <span class="info-label">占用内存:</span>
                <span class="info-value">{{ record.max_memory }} kbytes</span>
            </div>
        </div>
        <a-divider />
        <div class="code-container text-sm">
            <pre>
                <code class="diy">{{ record.code.toString() }}</code>
                <a-button type="text" class="code-copy" @click="copy(record.code); message.success('内容已复制到粘贴板')">
                    <template #icon>
                        <CopyIcon style="width: 1.25rem;height: 1.25rem" />
                    </template>
</a-button>
</pre>
        </div>
        <a-divider />
        <h2>测评点详情</h2>
        <a-table :data-source="record.judge_result" :columns="columns" :pagination="false" class="test-point-table">
            <template #bodyCell="{ column, text }">
                <template v-if="column.key === 'status'">
                    <span style="font-weight: bold;" :style="{ color: getTextColor(text) }">{{ text }}</span>
                </template>
            </template>
        </a-table>
    </a-card>
</template>

<style scoped>
.info-container {
    padding: 20px;
    background-color: #f0f7ff;
    border-radius: 8px;
    margin-bottom: 20px;
}

.info-row {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
}

.info-label {
    font-weight: bold;
    color: #2f54eb;
    width: 100px;
}

.info-value {
    color: #333;
}

.code-container {
    width: 100%;
    position: relative;
    display: inline-block;
}

.code-container:hover .code-copy {
    opacity: 1;
}

.code-copy {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 10;
    background-color: transparent;
    border: none;
    margin: 1.875rem 0.625rem 1.25rem;
    padding: 0.3125rem;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.3s ease;
}
.code-copy:active {
    box-shadow: none;
    transform: scale(0.95);

}
pre {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

.test-point-table {
    margin-top: 20px;
    border-radius: 8px;
    overflow: hidden;
}

.test-point-table .ant-table-thead>tr>th {
    background-color: #2f54eb;
    color: white;
    font-weight: bold;
}

.test-point-table .ant-table-tbody>tr>td {
    vertical-align: middle;
    transition: background-color 0.3s ease;
}

.test-point-table .ant-table-tbody>tr:hover>td {
    background-color: #f0f7ff;
}
</style>
